<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/2/7
 * Time: 17:09
 */
declare(strict_types=1);

namespace App\Process;
use App\Service\MchAccountAccessService;
use App\Task\MchAccountAccessTask;
use App\Utils\CustomUtils;
use App\Utils\RedisUtils;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Process\AbstractProcess;
use Hyperf\Utils\ApplicationContext;

/**
 * Class MchAccountAccessProcess
 * @package App\Process
 */
class MchAccountAccessProcess extends AbstractProcess
{

    /**
     * @Inject()
     * @var MchAccountAccessService
     */
    private $MchAccountAccessService;

    public function handle(): void
    {
        while(true){
            // 读取Redis，出栈一条数据，进行处理
            $cache = RedisUtils::redisResource()->lPop("MchAccountAccessList");
            if(!$cache)
            {
                sleep(1);
            }
            else
            {
                try {
//                    $container = ApplicationContext::getContainer();
//                    $task = $container->get(MchAccountAccessTask::class);
//                    $task->handle($cache);

                    $this->doHandle($cache);

                } catch (\Throwable $th) {
                    CustomUtils::xzxDebug($th->getMessage());
                }
            }
        }
    }

    public function doHandle($cache) {
        $param = unserialize($cache);
        if (!isset($param['mchAccountId']) || empty($param['mchAccountId'])) {
            CustomUtils::xzxDebug("mchAccountId 缺失");
            return [];
        }
        if (!isset($param['accessType']) || empty($param['accessType'])) {
            CustomUtils::xzxDebug("accessType 缺失");
            return [];
        }
        CustomUtils::xzxDebug($param);
        $this->MchAccountAccessService->handle($param);
    }

}